﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TasksModel
{
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://mp.tasks.com/2010/")]
    public class TaskAttachment : IdentifiedObject
    {
        #region Properties

        private string _fileName;

        /// <summary>
        /// IO file name with extension.
        /// </summary>
        public string FileName
        {
            [System.Diagnostics.DebuggerStepThrough]
            get { return _fileName; }
            set
            {
                if (!object.ReferenceEquals(_fileName, value))
                {
                    _fileName = value;
                    NotifyPropertyChanged("FileName");
                }
            }
        }

        private string _description;

        /// <summary>
        /// Optional.
        /// </summary>
        public string Description
        {
            [System.Diagnostics.DebuggerStepThrough]
            get { return _description; }
            set
            {
                if (!object.ReferenceEquals(_description, value))
                {
                    _description = value;
                    NotifyPropertyChanged("Description");
                }
            }
        }

        private byte[] _content;

        /// <summary>
        /// Do not modify array content.
        /// </summary>
        public byte[] Content
        {
            [System.Diagnostics.DebuggerStepThrough]
            get { return _content; }
            set
            {
                if (!object.ReferenceEquals(_content, value))
                {
                    _content = value;
                    NotifyPropertyChanged("Content");
                }
            }
        }

        public string UriSchema
        {
            [System.Diagnostics.DebuggerStepThrough]
            get { return BaseSchema + "attachment"; }
        }

        #endregion

        #region Constructors

        public TaskAttachment(Guid id)
        {
            ID = id;
        }

        public TaskAttachment()
            : this(Guid.NewGuid())
        {
        }

        /// <summary>
        /// Deep copy. Does not copy events.
        /// </summary>
        public TaskAttachment(TaskAttachment ta)
            : this(ta.ID)
        {
            _description = ta._description;
            _fileName = ta._fileName;
            if (ta._content != null)
            {
                _content = new byte[ta._content.Length];
                Array.Copy(ta._content, _content, ta._content.Length);
            }
            Timestamp = ta.Timestamp;
        }

        #endregion

        /// <summary>
        /// Open file by system shell default program.
        /// After use, delete Process.StartInfo.FileName
        /// </summary>
        /// <param name="tempPath">path to save content to</param>
        /// <returns>started process</returns>
        public System.Diagnostics.Process OpenFile(string tempPath)
        {
            if (_content == null)
                throw new InvalidOperationException("Attachment Content is empty");
            if (string.IsNullOrEmpty(_fileName))
                throw new InvalidOperationException("Attachment FileName is empty");
            string loc = System.IO.Path.Combine(tempPath, _fileName);
            System.IO.File.WriteAllBytes(loc, _content);
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.FileName = loc;
            process.Start();
            return process;
        }

        /// <summary>
        /// Save file contents to provided path.
        /// </summary>
        /// <param name="path">file path</param>
        public void SaveTo(string path)
        {
            System.IO.File.WriteAllBytes(path, _content);
        }
    }
}